//
//  DiscoveryActicleViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/9/2.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "DiscoveryActicleViewController.h"
#import "DisconveryActicleCell.h"
#import "DiscoveryWebViewController.h"


@interface DiscoveryActicleViewController ()

@property (nonatomic, strong) NSMutableArray *datas;


@end

@implementation DiscoveryActicleViewController

#pragma mark - view life cycle

- (void)setupUI {
    
    {
        self.baseNavView.hidden = YES;
    }
    
}

- (void) setupLayout {
    
    {
        self.reloadBtn.mj_y -= 100;
        self.reloadLab.mj_y -= 100;
    }

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(1);
        make.right.left.bottom.mas_equalTo(0);
    }];
}



- (void)initData {
    
    FoundActicleListParam *param = [[FoundActicleListParam alloc] init];
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    param.page = page;
    param.limit = @10;
    param.articleType = self.type;
    
    [self requestQSActicleDataByParam:param];
    
}

- (void)requestQSActicleDataByParam:(FoundActicleListParam *)param{
    
    @weakify(self);
    [[[MeatFoundApi sharedInstance] getArticleDetailListByParams:param] subscribeNext:^(FoundActicleListModel * _Nullable model) {
        @strongify(self);
        
        if (self.isRefresh) {
            
            [self endRefresh];
            if (self.datas.count >0) {
                [self.datas removeAllObjects];
            }
        }
        
        [model.list enumerateObjectsUsingBlock:^(FoundActicleListItem * _Nonnull item, NSUInteger idx, BOOL * _Nonnull stop) {
            
            CGFloat width = 0;
            
            if (!item.materialType) { //无图
                item.myType = @0;
                width = KScreenWidth - 30;
                item.height = 108;

            }else{
                
                if ([item.materialType integerValue] == 2) { //视频
                    item.myType = @3;
                    width = KScreenWidth - 30;
                    item.height = 281;
                }else{
                    
                    if ([item.templateType integerValue] == 2 ) {
                        item.myType = @2;
                        width = KScreenWidth - 30;
                        item.height = 195;

                    }else{
                        item.myType = @1;
                        width = KScreenWidth - 170;
                        item.height = 108;
                    }
                }
            }
            
            [self.datas addObject:item];
        }];
        
        if ([model.currPage intValue] >= [model.totalPage intValue]) {
            [self endLoadMoreMoData];
        }else{
            [self endLoadMore];
        }

        if (self.datas.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    }error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}


//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}

//上拉加载更多
- (void)loadMore
{
    [super loadMore];
    FoundActicleListParam *param = [[FoundActicleListParam alloc] init];
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    param.page = page;
    param.limit = @10;
    param.articleType = self.type;
    [self requestQSActicleDataByParam:param];
}


#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.datas.count) {
        FoundActicleListItem *item = self.datas[indexPath.row];
        return item.height;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DisconveryActicleCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"DisconveryActicleCell"];
    
    if (indexPath.row < self.datas.count) {

        FoundActicleListItem *item = self.datas[indexPath.row];
        cell.item = item;
        
        @weakify(self);
        cell.dealPlayerBlock = ^{
            
        };
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XXTGHomeWebViewController * vc = [[XXTGHomeWebViewController alloc]init];
    vc.urlString = URL_HTML_MineOnlineCustomerServiceURL;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter and setter

- (void)registerTableViewCell
{
    self.tableView.separatorColor = mIconLineColor;
    [self.tableView registerClass:[DisconveryActicleCell class] forCellReuseIdentifier:@"DisconveryActicleCell"];
}


- (NSMutableArray *)datas{
    
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}


#pragma mark - notifications methods

- (void)addNotifications{
    
    [kNotificationCenter addObserver:self selector:@selector(test:) name:NotificationFoundActicleSearch object:nil];
}

- (void)removeNotifications{
    
    [kNotificationCenter removeObserver:self name:NotificationFoundActicleSearch object:nil];
}

- (void)test:(NSNotification *)notification{
    
    NSLog(@"__________-    %@", notification.object);
    
    FoundActicleListParam *param = [[FoundActicleListParam alloc] init];
    param.page = @1;
    param.limit = @10;
//    param.memberId = kMemberId;
//    param.qsArticleId = @1;
    param.articleType = self.type;
    param.textStr = notification.object;
    
    [self requestQSActicleDataByParam:param];

}


@end
